package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IUserRegisterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description：
 * @Author： 黄武
 * @Date： 2022/12/3 10:06
 */
@RestController
@RequestMapping("/user")
public class RegisterController {

    @Autowired
    IKaptchaService kaptchaService;

    @Autowired
    IUserRegisterService registerService;


    /**
     *  用户注册接口
     * @return
     */
    @PostMapping("/register")
    public ResponseData register(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest request){
        // 获取用户信息
        String userName = userRegisterRequest.getUserName();
        String userPwd = userRegisterRequest.getUserPwd();
        String captcha = userRegisterRequest.getCaptcha();
        String email = userRegisterRequest.getEmail();

        // 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setCode(captcha);
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaService.validateKaptchaCode(kaptchaCodeRequest);

        // 向用户表中插入数据
        UserRegisterRequest registerRequest = new UserRegisterRequest();
        registerRequest.setEmail(email);
        registerRequest.setUserName(userName);
        registerRequest.setUserPwd(userPwd);
        registerService.register(registerRequest);

        // 到达这里如果还没有异常，说明注册成功
       return new ResponseUtil<>().setData(null);
    }

    /**
     *  注册账号后激活的功能
     * @param request
     * @return
     */

    @GetMapping("/verify")
    public ResponseData verify(HttpServletRequest request){
        // 获取激活信息
        String username = request.getParameter("username");
        String uuid = request.getParameter("uuid");

        // 判空
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(uuid)){
            throw new ValidateException(UserRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    UserRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }

        // 更新用户表中的用户激活状态
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUsername(username);
        userVerifyRequest.setUuid(uuid);
        registerService.verify(userVerifyRequest);

        return new ResponseUtil<>().setData(null);
    }
}
